package com.galen.kaptch;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 验证码过滤器实现
 *
 * @author GalenGao
 * @version Id: KaptchFilter.java, v 0.1 2022/6/25 15:52 GalenGao Exp $$
 */
public class KaptchaFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

        if (!request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
            throw new AuthenticationServiceException("HttpServletRequest contentType not supported: " + request.getContentType());
        }
        try {
            // 前后端分离项目使用
            Map<String, String> map = new ObjectMapper().readValue(request.getInputStream(), Map.class);

            String username = map.get(getUsernameParameter());
            String password = map.get(getPasswordParameter());
            String rememberMe = map.get("rememberMe");

            if (!StringUtils.hasLength(rememberMe)) {
                throw new AuthenticationServiceException("rememberMe attributes can't not null");
            }

            String kaptcha = (String) request.getSession().getAttribute("kaptcha");
            String parameter = map.get("kaptcha");
            if (parameter != null && parameter.equals(kaptcha)) {
                // 向请求域中设置remember的值
                request.setAttribute("rememberMe", rememberMe);
                UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
                setDetails(request, authRequest);
                this.getAuthenticationManager().authenticate(authRequest);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        throw new AuthenticationServiceException("verify code is incorrect");
    }

    /**
     * 原始登录方式
     *
     * @param request request
     * @return Boolean
     */
    private Boolean originalLogin(HttpServletRequest request) {
        // 获取存储在HttpSession中的验证码
        String kaptcha = (String) request.getSession().getAttribute("kaptcha");
        String parameter = request.getParameter("kaptcha");
        if (parameter != null && parameter.equals(kaptcha)) {
            return true;
        }
        return false;
    }
}
